import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  Request,
  Response,
  Headers,
  HttpCode,
  Session,
} from '@nestjs/common'
import { UserService } from './user.service'
import { CreateUserDto } from './dto/create-user.dto'
import { UpdateUserDto } from './dto/update-user.dto'

import * as svgCaptcha from 'svg-captcha'
import session from 'express-session'
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get('code')
  findAll(@Request() req, @Response() res, @Session() session) {
    const captcha = svgCaptcha.create({
      size: 4, //生成几个验证码
      fontSize: 50, //文字大小
      width: 100, //宽度
      height: 34, //高度
      background: '#cc9966', //背景颜色
    })
    console.log(captcha)
    session.code = captcha.text
    res.type('image/svg+xml')
    res.send(captcha.data)
  }

  @Post('create')
  createUser(@Body() body, @Session() session) {
    console.log(body, session.code)
    if (session?.code?.toLocaleLowerCase() === body?.code?.toLocaleLowerCase()) {
      return {
        code: 200,
        message: body,
        msg: '验证码正确',
      }
    } else {
      return {
        code: 200,
        message: body,
        msg: '验证错误',
      }
    }
  }

  @Get(':id')
  @HttpCode(500)
  findOne(@Param() id, @Headers() headers) {
    console.log(headers)

    return {
      code: 200,
      param: id,
    }
  }

  // @Get()
  // createCode(@Req() req, @Res() res) {
  //   console.log('code');

  //   // const captcha = svgCaptcha.create({
  //   //   size: 4, //生成几个验证码
  //   //   fontSize: 50, //文字大小
  //   //   width: 100, //宽度
  //   //   height: 34, //高度
  //   //   background: '#cc9966', //背景颜色
  //   // })
  //   return { code:666 }
  //   // req.session.code = captcha.text //存储验证码记录到session
  //   // res.type('image/svg+xml')
  //   // res.send(captcha.data)
  // }

  // @Patch(':id')
  // update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
  //   return this.userService.update(+id, updateUserDto);
  // }

  // @Delete(':id')
  // remove(@Param('id') id: string) {
  //   return this.userService.remove(+id);
  // }
}
